package com.erhirent.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.erhairent.tools.DBHelp;
import com.erhairent.tools.ResultSetHandler;
import com.erhairent.tools.Template;
import com.erhirent.bean.carUsers;
import com.erhirent.dao.IusersDao;
import com.erhirent.view.loginView;

public class UsersDaoImpl implements IusersDao {

	// 登录时判断账号密码
	public boolean isSame(String Name, String Pwd) {

		ArrayList<carUsers> list = new ArrayList<>();
		Connection conn = DBHelp.getInstance().getConnection();

		String sql = "select user_name ,user_pwd from erhai_users ";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rset = ps.executeQuery();
			while (rset.next()) {
				carUsers user = new carUsers();
				user.setName(rset.getString("user_name"));
				user.setPwd(rset.getString("user_pwd"));
				list.add(user);

			}
		} catch (SQLException e) {
			System.out.println("获取连接失败!");
		}
		for (int i = 0; i < list.size(); i++) {

			if (list.get(i).getName().equals(Name) && list.get(i).getPwd().equals(Pwd)) {
				return true;

			}

		}

		System.out.println("密码或账号错误,请重新输入");

		return false;

	}

	// 向数据库加入注册用户
	public boolean addUsers(carUsers users) {
		Connection conn = DBHelp.getInstance().getConnection();
		String sql = "insert into erhai_users(user_id,user_name,user_pwd) values(user_seq.nextval,?,?)";
		PreparedStatement ps;
		try {
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			System.out.println("重置自动提交.");
		}
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, users.getName());
			ps.setString(2, users.getPwd());
			ps.executeUpdate();
			conn.commit();
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return true;
	}

	// 注册时判断用户是否已存在
	public boolean isname(String name) {
		ArrayList<carUsers> list = new ArrayList<>();
		Connection conn = DBHelp.getInstance().getConnection();
		String sql = "select user_name from erhai_users ";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rset = ps.executeQuery();
			while (rset.next()) {
				carUsers user = new carUsers();
				user.setName(rset.getString("user_name"));
				list.add(user);
			}
		} catch (SQLException e) {
			System.out.println("获取连接失败!");
		}
		for (int i = 0; i < list.size(); i++) {

			if (list.get(i).getName().equals(name)) {
				System.out.println("账号已存在,请重新输入");
				return false;
			}
		}
		return true;
	}

	// 得到用户id
	public int GetUserId(String name) {
		ArrayList<carUsers> list = new ArrayList<>();
		Connection conn = DBHelp.getInstance().getConnection();
		String sql = "select user_id ,user_name from erhai_users ";

		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ResultSet rset = ps.executeQuery();
			while (rset.next()) {
				carUsers user = new carUsers();
				user.setuserId(rset.getInt("user_id"));
				user.setName(rset.getString("user_name"));

				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int user_id = 0;
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getName().equals(name)) {
					user_id = list.get(i).getuserId();

					break;
				}
			}
		}

		return user_id;
	}

	@Override
	public carUsers getuser(String name) {
		carUsers user = new carUsers();

		Template<carUsers> temp = new Template<carUsers>();

		String sql = "select * from t_user where user_name=?";

		temp.testQuery(sql, new ResultSetHandler<carUsers>() {

			@Override
			public carUsers handler(ResultSet rset) {
				try {
					rset.next();
					user.setuserId(rset.getInt("user_id"));
					user.setName(rset.getString("user_name"));
					user.setPwd(rset.getString("user_password"));

				} catch (SQLException e) {
					System.out.println("请重新输入");
					new loginView().showView();
				}

				return user;
			}

		}, name);
		return user;

	}

}
